define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val(),
                    status = $('.actived').data('status'),order_id;

                getData();

                $('.gc-tab-item').click(function(){
                    $('#order_list').html('');
                    $(this).addClass('actived').siblings().removeClass('actived');
                    status = $(this).data('status');
                    getData();
                })

                $(document).on('click','#pickUp',function(){
                    order_id = $(this).data('order_id');
                    fm.confirm('<p class="text-16 confirm_t">确认提示</p><p>你确定要提取这些商品么？</p>', function (callback) {
                        if(callback == 1){
                            pickUp();
                        }
                    }, 'border-top: 4px solid #00a5f3;min-width: 64%;');
                })

                function pickUp(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/orderPickup',
                        type: 'post',
                        data: {store_id: store_id,order_id:order_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                setTimeout(function(){
                                    status = 2;
                                    $('.tab-'+status).addClass('actived').siblings().removeClass('actived');
                                    $('#order_list').html('');
                                    getData();
                                },500);
                            }else{
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }


                function getData(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/userCenter',
                        type: 'post',
                        data: {store_id: store_id,status:status},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                var html = '',status_html = '',btn = '';

                                if(data.cartCount > 0){
                                    $('.tips').html(data.cartCount).removeClass('hide');
                                }

                                $('.user-head').html('<img src="'+ data.user.avatar +'"/>');
                                $('.user_name').html(data.user.real_name);

                                if(data.orderGoodsData.length > 0){
                                    $.each(data.orderGoodsData,function(i,k){
                                        if(status == 1){
                                            btn = '<a href="javascript:;" class="btn-default gc-ft-center" id="pickUp" data-order_id="'+ k.order_id +'">确定取货</a>';
                                            status_html = '<p class="gc-ft-yellow status">待取货</p>';
                                        }else{
                                            btn = '<a href="/restaurant/TrustworthyShop/commentshow/store_id/'+ store_id +'/order_id/'+ k.order_id +'" class="btn-default gc-ft-center">马上评价</a>';
                                            status_html = '<p class="gc-ft-yellow status">交易成功</p>';
                                        }
                                        var goods = '';
                                        $.each(k.order_goods,function(i,k){
                                            goods += '<a href="/restaurant/TrustworthyShop/gooddetail/store_id/'+ store_id +'/goods_id/'+ k.goods_id +'" class="gc-flex gc-flex-lc gc-block-line">' +
                                                '<div class="item-img"><img src="'+ k.goods_thumb +'"></div>' +
                                                '<div class="item-msg gc-flex-item"><div class="gc-flex gc-flex-between">' +
                                                '<p class="item-name gc-ft-normal">'+ k.goods_name +'</p>' +
                                                '</div><span class="gc-ft-red price">￥'+ k.goods_price +'</span></div></a>';
                                        });

                                        html += '<div class="gc-block full item list-item"><div class="gc-flex gc-flex-between gc-block-line msg-title">' +
                                            '<p class="gc-ft-deepgray ellipsis gc-flex-item"><span>'+ k.name +'</span></p>'+ status_html + '</div>'+ goods + '<div class="gc-flex gc-flex-between gc-block-line">' +
                                            '<p class="gc-ft-deepgray">合计：<span class="gc-ft-red gc-ft-lg price">￥'+ k.order_amount +'</span> 共计'+ k.order_nums +'件菜品</p>'+ btn +'</div></div>'
                                    })

                                    $('#order_list').append(html)
                                } else{
                                    html = '<div class="full"><div class="empty"><img src="/assets/restaurant/TrustworthyShop/images/empty2.png"/></div>' +
                                        '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">暂无该分类的订单</p></div>';
                                    $('#order_list').html(html);
                                }
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

            });
        });
    };
});
